package com.wl.api.service.system.impl;

import static com.wl.api.common.utils.PageInfo.transfromPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.PageInfo;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.dto.system.SecurityModuleDTO;
import com.wl.api.dto.system.SecurityRoleDTO;
import com.wl.api.mapper.system.SecurityModuleMapper;
import com.wl.api.model.system.SecurityModule;
import com.wl.api.model.system.SecurityRole;
import com.wl.api.model.system.SecurityRolePermission;
import com.wl.api.service.system.ISecurityModuleService;
import com.wl.api.service.system.ISecurityRolePermissionService;

/**
 *
 * SecurityModule 表数据服务层接口实现类
 *
 */
@Service
public class SecurityModuleServiceImpl extends ServiceImpl<SecurityModuleMapper, SecurityModule> implements ISecurityModuleService {
	
	@Autowired
	ISecurityRolePermissionService rolePermissionService;
	
	//sn一旦创建不可修改sn
	@Override
	public int updateModuleData(SecurityModule param) {
		Assert.notNull(param.getId());
		param.setSn(null);
		updateById(param);
		return param.getId().intValue();
	}
	@Transactional
	public void deleteModule(SecurityModule param) {
		rolePermissionService.update(SecurityRolePermission.builder().updateId(param.getId()).updateUser(param.getUpdateUser()).isDeleted(1).build(), new EntityWrapper<SecurityRolePermission>(SecurityRolePermission.builder().isDeleted(0).moduleId(new Long(param.getId())).build()));
		deleteById(param.getId());
	}

	/**
	 * 获取所有的功能以树结构的方式展示	
	 */
	
	public List<SecurityModuleDTO> getModuleList(SecurityModuleDTO param) {
		List<SecurityModule> list = selectList(new EntityWrapper<SecurityModule>());
		if (CollectionUtil.isEmpty(list)) {
			return new ArrayList<SecurityModuleDTO>();
		}
		List<SecurityModuleDTO> sourceList = BeanUtils.copyProperties(list, SecurityModuleDTO.class);
		List<SecurityModuleDTO> rootList = sourceList.parallelStream().filter(s -> !Objects.isNull(s.getParentId()) && Objects.equals(s.getParentId(), Objects.isNull(param.getId())? 0: param.getId())).collect(Collectors.toList());
		if (CollectionUtils.isEmpty(rootList)) {
			return new ArrayList<SecurityModuleDTO>();
		}
		for (SecurityModuleDTO m : rootList) {
			m.setChildren(makeChildren(sourceList, m));
		}
		return null;
	}
	
	private List<SecurityModuleDTO> makeChildren(List<SecurityModuleDTO> sourceList, SecurityModuleDTO parent) {
		if (CollectionUtils.isEmpty(sourceList)) {
			return null;
		}
		List<SecurityModuleDTO> tmp = new ArrayList<SecurityModuleDTO>();
		for (SecurityModuleDTO source : sourceList) {
			if (Objects.equals(parent.getId(), source.getParentId())) {
				tmp.add(SecurityModuleDTO.builder().id(source.getId()).url(source.getUrl()).sn(source.getSn()).description(source.getDescription()).name(source.getName()).children(makeChildren(sourceList, source)).build());
			}
		}
		
		sourceList.removeAll(tmp);
		return tmp;
	}
	
	
	@Override
	public PageInfo<SecurityModuleDTO> getModuelListPage(SecurityModuleDTO param) {
		Page<SecurityModule> page = selectPage(new Page<>(param.getPage(), param.getPageSize()), new Wrapper<SecurityModule>() {

			@Override
			public String getSqlSegment() {
				StringBuilder sb = new StringBuilder();
				sb.append(" where 1=1 ");
				if (param.getId() != null) {
					sb.append(" and id = ");
					sb.append(param.getId());
				}
				if (!StringUtil.isEmpty(param.getName())) {
					sb.append(" and name  like '");
					sb.append(param.getName());
					sb.append("'");
				}
				return sb.toString();
			}
		});
		return  transfromPage(SecurityModuleDTO.class, page);
	}


}